package com.iflytek.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.iflytek.common.Result;
import com.iflytek.dto.LogDTO;
import com.iflytek.dto.PageDTO;
import com.iflytek.mapper.LogMapper;
import com.iflytek.model.Log;
import com.iflytek.service.LogService;
import com.iflytek.service.RoleService;
import com.iflytek.utils.CommonUtil;
import com.iflytek.utils.CopyUtil;
import com.iflytek.vo.LogVO;
import com.iflytek.vo.PageVO;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 日志表 服务实现类
 * </p>
 *
 * @author h_t
 * @since 2023-09-23
 */
@Service
public class LogServiceImpl extends ServiceImpl<LogMapper, Log> implements LogService {

    @Resource
    private LogMapper logMapper;
    @Resource
    private LogService logService;

    @Resource
    private RoleService roleService;

    /**
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Result<List<LogVO>> findAll() {
        QueryWrapper<Log> queryWrapper = new QueryWrapper<>();
        List<Log> logList = logMapper.selectList(queryWrapper);
        List<LogVO> logVOList = CopyUtil.copyList(logList, LogVO.class);
        return Result.success(logVOList);
    }

    /**
     * saveLog
     * @param dto
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Result<Boolean> saveLog(LogDTO dto) {
        //Log log = new Log();
        if (dto == null) {
            return Result.error("1", "内容为空");
        }
        Log log = CopyUtil.copy(dto, Log.class);
        if (StringUtils.isEmpty(log.getId())) {
            if (StringUtils.isEmpty(log)) {
                return Result.error("1", "保存失败");
            }
//            log.setId(UuidUtil.getShortUuid());

        }
        logMapper.insert(log);
        return Result.success(true);
    }

    /**
     * updateLog
     * @param dto
     * @return
     */
    @Override
    public Result<Boolean> updateLog(LogDTO dto) {
        Log log = new Log();
        BeanUtils.copyProperties(dto, log);
        int count = logMapper.updateById(log);
        if (count > 0) {
            return Result.success(true);
        } else {
            return Result.error("1", "更新失败");
        }
    }

    /**
     * @param id
     * @return
     */
    @Override
    public Result<LogVO> findById(Integer id) {

        if (id == null) {
            Result.error("401", "id为空");
        }
        Log log = logMapper.selectById(id);
        LogVO logVO = CopyUtil.copy(log, LogVO.class);

        return Result.success(logVO);
    }


    /**
     * @param id
     * @return
     */
    @Override
    public Result<LogVO> deleteById(Integer id) {

        if (id == null) {
            Result.error("401", "id为空");
        }
        int i = logMapper.deleteById(id);
        if (i > 0) {
            Result.success();
        } else {

            Result.error("204", "错误");
        }
        return Result.success();
    }

    /**
     * @param dto
     * @return
     */
    @Override
    public Result<PageVO<LogVO>> findPage(PageDTO<LogDTO> dto) {
        QueryWrapper<Log> queryWrapper = new QueryWrapper();

        if (!CommonUtil.isEmpty(dto.getName())) {
            queryWrapper.lambda().like(Log::getContent, dto.getName());
        }
        if (dto.getPageNum() == null) {
            dto.setPageNum(1);
        }
        if (dto.getSize() == null) {
            dto.setSize(5);
        }
        //分页
        PageHelper.startPage(dto.getPageNum(), dto.getSize());

        List<Log> logs = logMapper.selectList(queryWrapper);

        List<LogVO> logVOList = new ArrayList<>();
        logVOList = CopyUtil.copyList(logs, LogVO.class);

        PageInfo<Log> logPageInfo = new PageInfo<>(logs);
        PageVO<LogVO> pageVO = new PageVO<>();
        BeanUtils.copyProperties(dto, pageVO);
        pageVO.setTotal(logPageInfo.getTotal());
        pageVO.setRecords(logVOList);
        return Result.success(pageVO);
    }

}
